package com.yp.lession73.slice;

import com.yp.lession73.ResourceTable;
import com.yp.lession73.entity.City;
import com.yp.lession73.provider.CityProvider;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;

import java.util.ArrayList;
import java.util.List;

public class MainAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        //1.获得listContainer 对象
        ListContainer listContainer = (ListContainer) findComponentById(ResourceTable.Id_myContanier);
        //2.获得数据
        List<City> listData = createListData();
        //3.实例化cityProvider
        CityProvider cityProvider =new CityProvider(listData,this);
        //4.把cityProvider 提供组件加入listontainer当中
        listContainer.setItemProvider(cityProvider);
        //5. 监控组件数据的加载
        listContainer.setBindStateChangedListener(new Component.BindStateChangedListener() {
            @Override
            public void onComponentBoundToWindow(Component component) {
                //调用provider的数据更新方法
                cityProvider.notifyDataChanged();

            }

            @Override
            public void onComponentUnboundFromWindow(Component component) {

            }
        });
    }
    private List<City> createListData(){
        List<City> list = new ArrayList<>();
        for (int i = 0; i < 100; i++){
            list.add(new City("城市:"+i));
        }
        return list;
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
